package gui;

import java.awt.BorderLayout;
import java.awt.Dimension;
import java.io.File;

import javax.swing.BoxLayout;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.UIManager;
import javax.swing.UnsupportedLookAndFeelException;
import javax.swing.border.EmptyBorder;

import csv_parser.DataHandler;

public class Frame extends JFrame {

	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;
	JPanel panel;
	JPanel nnPanel;
	ReadyStatePanel classifierReady;
	NotReadyStatePanel classifierNotReady;
	StatisticsPanel statisticPanel;
	ResultPanel resultPanel;
DataHandler handler;
	private Frame() {
		setUI();
		setTitle("Klasifikator");
		initPanel();
		initNNPanel();
		initData();
		getContentPane().add(panel, BorderLayout.CENTER);
		setSize(new Dimension(500, 700));
		setResizable(false);
		setLocationRelativeTo(null);
		setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		setVisible(true);
	}

	private void initData() {
		handler = new DataHandler();
		handler.readData();
	}

	private void initNNPanel() {
		classifierNotReady = new NotReadyStatePanel();
		classifierReady = new ReadyStatePanel();
		resultPanel = new ResultPanel();
		statisticPanel = new StatisticsPanel();

		File file = new File("thetas.mat");
		if (!file.exists()) {
			classifierNotReady.setVisible(true);
		} else {
			classifierReady.setVisible(true);
		}
		panel.add(classifierNotReady);
		panel.add(classifierReady);
		panel.add(resultPanel);
		panel.add(statisticPanel);
	}

	private void initPanel() {
		panel = new JPanel();
		panel.setLayout(new BoxLayout(panel, BoxLayout.PAGE_AXIS));
		panel.setBorder(new EmptyBorder(10, 10, 10, 10));
	}

	private void setUI() {
		try {
			UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
		} catch (ClassNotFoundException e) {
			e.printStackTrace();
		} catch (InstantiationException e) {
			e.printStackTrace();
		} catch (IllegalAccessException e) {
			e.printStackTrace();
		} catch (UnsupportedLookAndFeelException e) {
			e.printStackTrace();
		}
	}

	public static void main(String[] args) {
		Frame.getFrame();
	}

	private static Frame frame;

	public static Frame getFrame() {
		if (frame == null) {
			frame = new Frame();
		}
		return frame;
	}

	public void setStatisticPanelVisible(boolean b) {
		this.statisticPanel.setVisible(b);
	}

	public void setResultPanelVisible(boolean b) {
		this.resultPanel.setVisible(b);
	}

	public void setReadyStatePanelVisible(boolean b) {
		this.classifierReady.setVisible(b);
	}

	public void setNotReadyStatePanelVisible(boolean b) {
		this.classifierNotReady.setVisible(b);
	}
}
